using System;
using TTEngine.TTMath;
using Silk.NET.OpenGLES;
using TTEngine.Resource;
namespace TTEngine.Graphics
{
    public class Texture2DArray : ResourceObj, ITexture
    {
        protected uint _texobj;
        protected byte _layer;
        protected TextureType _format;

        protected uint _width;
        protected uint _height;

        public uint TexID => _texobj;

        public uint Width => _width;

        public uint Height => _height;

        public TextureType format
        {
            get => _format;
            private set => _format = value;
        }

        public byte LayerCount => _layer;

        public bool MipMap
        {
            get;
            private set;
        }

        public bool IsArray()
        {
            return true;
        }



        protected override void OnDestory()
        {
            if (_texobj != 0)
            {
                TTEngine.Runtime.activeGL.DeleteTexture(_texobj);
                _texobj = 0;
            }
        }

        public Texture2DArray(ResourcePackage package, uint width, uint height, byte layercount, TextureType format, bool mipmap = false)
            : base(package)
        {
            InitTexture2DArray(width, height, layercount, format, mipmap = false);
        }
        void InitTexture2DArray(uint width, uint height, byte layercount, TextureType format, bool mipmap = false)
        {
            _format = format;
            _layer = layercount;
            _texobj = TTEngine.Runtime.activeGL.GenTexture();
            _width = width;
            _height = height;
            MipMap = mipmap;

            TTEngine.Runtime.activeGL.BindTexture(TextureTarget.Texture2DArray, _texobj);
            // _gl.PixelStorei(PixelStoreParameter.UnpackPremultiplyAlpha, 0);
            // _gl.PixelStorei(PixelStoreParameter.UnpackFlipY, 0);

            if (mipmap)
            {
                TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Linear);
                TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Linear);
            }
            else
            {
                TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
                TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureMagFilter, (int)TextureMinFilter.Nearest);
            }
            TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
            TTEngine.Runtime.activeGL.TexParameter(TextureTarget.Texture2DArray, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);


            //_gl.BindTexture(TextureTarget.Texture2DArray, _texobj);
            // _gl.TexImage2DArray(0, format, width, height, layercount);
            bool gray = format <= TextureType.R_Float;
            InternalFormat glformat = gray ? InternalFormat.Red : InternalFormat.Rgba;
            PixelFormat pformat = gray ? PixelFormat.Red : PixelFormat.Rgba;
            PixelType ptype = PixelType.UnsignedByte;
            if (format == TextureType.RGBA_Float || format == TextureType.R_Float)
                ptype = PixelType.Float;
            unsafe
            {
                void* ptr = null;
                {
                    TTEngine.Runtime.activeGL.TexImage3D(TextureTarget.Texture2DArray, 0, glformat, (uint)width, (uint)height, (uint)layercount, 0, pformat, ptype, ptr);
                }
            }


            if (mipmap)
                TTEngine.Runtime.activeGL.GenerateMipmap(TextureTarget.Texture2DArray);
        }

        public bool IsRenderTarget()
        {
            return false;
        }
        public void UploadImageData(int layer, int x, int y, uint w, uint h, IImage data)
        {
            if (data.Width != this.Width || data.Height != this.Height)
                throw new Exception("error size.");
            if (data.Type == ImageFormat.Gray && this.format != TextureType.R_U8)
                throw new Exception("error format");
            if (data.Type == ImageFormat.RGBA && this.format != TextureType.RGBA_32)
                throw new Exception("error format");
            UploadImageData(layer, x, y, w, h, data.GetData());
        }
        public void UploadImageData<T>(int layer, int x, int y, uint w, uint h, T[] data) where T : unmanaged
        {
            TTEngine.Runtime.activeGL.BindTexture(TextureTarget.Texture2DArray, _texobj);

            bool gray = format <= TextureType.R_Float;
            InternalFormat glformat = gray ? InternalFormat.Red : InternalFormat.Rgba;
            PixelFormat pformat = gray ? PixelFormat.Red : PixelFormat.Rgba;
            PixelType ptype = PixelType.UnsignedByte;
            if (format == TextureType.RGBA_Float || format == TextureType.R_Float)
                ptype = PixelType.Float;
            unsafe
            {
                fixed (void* ptr = data)
                {
                    TTEngine.Runtime.activeGL.TexSubImage3D(TextureTarget.Texture2DArray, 0, x, y, layer, (uint)w, (uint)h, (uint)1, pformat, ptype, ptr);
                }
            }


            if (MipMap)
                TTEngine.Runtime.activeGL.GenerateMipmap(TextureTarget.Texture2DArray);
        }


    }
}